package com.myown.structure.impl;

import com.myown.structure.Stack;

import java.util.ArrayList;

/**
 * 栈 <p>
 *
 * <tt>ArrayList</tt>实现 <p>
 *
 * @author lincky
 * @version v1.0.0
 * @time 2017.02.21 17:55
 */
public class ArrayStack<E> extends AbstractCollection<E> implements Stack<E> {

    public ArrayStack() {
        super(new ArrayList<E>());
    }

    @Override
    public void push(E item) {
        add(item);
    }

    @Override
    public E pop() {
        E top = peek();
        remove(size() - 1);
        return top;
    }

    @Override
    public E peek() {
        if (empty()) {
            throw new IndexOutOfBoundsException(UNDERFLOW_BOUND_MSG);
        }
        return get(size() - 1);
    }

    @Override
    public String toString() {
        return "ArrayStack{" +
                "size=" + size() +
                ", arraystack=" + elements +
                '}';
    }

}
